package com.kunteng.exchangeboard.cotroller;

import com.kunteng.exchangeboard.mqtt.api.MQTTSenderHandler;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by ShiYun on 2016/9/26 0026.
 */
public class SetInfo extends HttpServlet {

    MQTTSenderHandler mqttSenderHandler = new MQTTSenderHandler();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String port = req.getParameter("port");
        String type = req.getParameter("operator");
        boolean flag = false;
        try {
            int p = Integer.parseInt(port);
            String status = "Enable";
            if (type.equals("0")) {
                status = "Disable";
            }
            flag = mqttSenderHandler.setPort(p, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter printWriter = resp.getWriter();
        printWriter.println(flag);
        printWriter.close();
    }
}
